package com.innovations.inn.controller;

import com.innovations.common.annotation.Log;
import com.innovations.common.core.controller.BaseController;
import com.innovations.common.core.domain.AjaxResult;
import com.innovations.common.core.page.TableDataInfo;
import com.innovations.common.enums.BusinessType;
import com.innovations.common.utils.poi.ExcelUtil;
import com.innovations.framework.web.service.TokenService;
import com.innovations.inn.domain.MessageNotification;
import com.innovations.inn.service.IMessageNotificationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 消息通知Controller
 *
 * @author dp
 * @date 2024-08-09
 */
@Api(tags = "消息通知")
@RestController
@RequestMapping("/inn/notification")
public class MessageNotificationController extends BaseController {
    @Autowired
    private IMessageNotificationService messageNotificationService;
    @Autowired
    TokenService tokenService;

    /**
     * 查询消息通知列表
     */
    @ApiOperation("查询消息通知列表")
    @PreAuthorize("@ss.hasPermi('inn:notification:list')")
    @GetMapping("/list")
    public TableDataInfo list(MessageNotification messageNotification) {
        startPage();
        List<MessageNotification> list = messageNotificationService.selectMessageNotificationList(messageNotification);
        return getDataTable(list);
    }

    /**
     * 导出消息通知列表
     */
    @PreAuthorize("@ss.hasPermi('inn:notification:export')")
    @Log(title = "消息通知", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MessageNotification messageNotification) {
        List<MessageNotification> list = messageNotificationService.selectMessageNotificationList(messageNotification);
        ExcelUtil<MessageNotification> util = new ExcelUtil<MessageNotification>(MessageNotification.class);
        util.exportExcel(response, list, "消息通知数据");
    }

    /**
     * 获取消息通知详细信息
     */
    @ApiOperation("获取消息通知详细信息")
    @PreAuthorize("@ss.hasPermi('inn:notification:query')")
    @GetMapping(value = "/{messageNotificationId}")
    public AjaxResult getInfo(@PathVariable("messageNotificationId") Long messageNotificationId) {
        return success(messageNotificationService.selectMessageNotificationByMessageNotificationId(messageNotificationId));
    }

    /**
     * 新增消息通知
     */
    @ApiOperation("新增消息通知")
    @PreAuthorize("@ss.hasPermi('inn:notification:add')")
    @Log(title = "消息通知", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody MessageNotification messageNotification) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        messageNotificationService.completeData(messageNotification, userId, 1);
        return toAjax(messageNotificationService.insertMessageNotification(messageNotification));
    }

    /**
     * 修改消息通知
     */
    @ApiOperation("修改消息通知")
    @PreAuthorize("@ss.hasPermi('inn:notification:edit')")
    @Log(title = "消息通知", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(HttpServletRequest request, @RequestBody MessageNotification messageNotification) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        messageNotificationService.completeData(messageNotification, userId, 2);
        return toAjax(messageNotificationService.updateMessageNotification(messageNotification));
    }

    /**
     * 删除消息通知
     */
    @ApiOperation("删除消息通知")
    @PreAuthorize("@ss.hasPermi('inn:notification:remove')")
    @Log(title = "消息通知", businessType = BusinessType.DELETE)
    @DeleteMapping("/{messageNotificationIds}")
    public AjaxResult remove(@PathVariable Long[] messageNotificationIds) {
        return toAjax(messageNotificationService.deleteMessageNotificationByMessageNotificationIds(messageNotificationIds));
    }
}
